var fs = require('fs'), system = require('system');
var index = 0, str_Url = [], unable_str ='', openURL, RenderUrlsToFile; 
if(system.args.length < 2) {
	console.log("the phantomjs usage is --> phantomjs XX.js theURLstring");
	phantom.exit();
}else {
	openURL = system.args[1];
	console.log('urlString : '+openURL);
	var str_Url = openURL.split(',');
	console.log(':'+str_Url.length);
}
if(str_Url.length > 100) {
	console.log("phantomjs run once only open 100 page");
}
//Render Multiple URLs to file

RenderUrlsToFile = function(urls, callbackPerUrl, callbackFinal) {
    var next, page, retrieve, webpage;
    webpage = require("webpage");
    page = null;
	getGoodsInfo = function() {
		var info_li = document.querySelectorAll("#attributes .attributes-list li");
		var array_info = {};
		for(var i = 0; i < info_li.length; i++) {
			var innerStr = info_li[i].innerHTML.replace(/&nbsp;/g, "").replace(/(^\s+)|(\s+$)/g, "");
			var tit = innerStr.split(":")[0];
			var content = innerStr.split(":")[1];
			array_info[tit] = content;
		}
		return array_info;
	};
    next = function(status, url) {
        page.close();
        callbackPerUrl(status, url);
        return retrieve();
    };
    retrieve = function() { 
		var url, startTime;
		if (urls.length > 0) {
			url = urls.shift();
			page = webpage.create();
			page.viewportSize = {
				width: 1280,
				height: 800
			};
			page.settings.resourceTimeout = 4000;
			page.settings.loadImages = false;
			page.settings.userAgent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36';
			page.onResourceTimeout = function(e) {
				unable_str = "can not open the page the url is: " + url + "\r\n";
				fs.write('unable.txt', unable_str, 'a');
				url = urls.shift();
			};
			return page.open(url, function(status) {
				var t, data_timer, time;
				console.log("open status :" + status);
                if (status == "success") {
					startTime = new Date().getTime();
					var tmOrTaobao = url.split('.')[1];
					var goodsId = url.split('?')[1].split('=')[1];
					var goods_data = {
						"img_link": null,
						"goods_info": null,
						"goods_size": null,
						"goods_color": null                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
					};
					//打开后发现商品已经下架
					var errorFlag = page.evaluate(function() {
						var errorName = document.getElementsByClassName('error-notice-hd')[0];
						if(errorName == undefined) {
							return false;
						}else {
							return true;
						}
					});
					if(errorFlag == true) {
						console.log("the goods id from the shelves");
						unable_str = "Goods from the shelves url is: " + url + "\r\n";
						fs.write('unable.txt', unable_str, 'a');
						return next(status, url);
					}else {
						return function() {
							var info = page.evaluate(getGoodsInfo);
							goods_data["goods_info"] = info;
							
							//get the goods size
							var size = page.evaluate(function(flag_str) {
								function hasClass(className,node){
									var classNames = node.className.split(/\s+/);
									for(var i = 0; i < classNames.length; i++){
										if(classNames[i] == className){
											return true;
										}
									}
									return false;
								}
								var select_array, size_array = {}, tmKey, tmValue; 
								if(flag_str == "taobao") { 
									var taobao_select_dl = document.getElementsByClassName("J_Prop")[0];
									if(taobao_select_dl  == undefined) {
										size_array["size"] = "未定义";
									}else {
										if(hasClass('J_Prop_Color', taobao_select_dl)){
											size_array["size"] = "未定义";
										}else {
											var m = taobao_select_dl.getElementsByTagName('dt')[0].innerHTML;
											select_array = taobao_select_dl.getElementsByTagName('span');
											var sizeArray = [];
											for(var i = 0; i < select_array.length; i++) {
												sizeArray[i] = select_array[i].innerHTML;
											}
											size_array[m] = sizeArray;
										}
									}
								}else {
									var tm_selector = document.getElementsByClassName("tm-sale-prop")[0];
									if(tm_selector == undefined) {
										size_array["size"] = "未定义";
									}else {
										if(hasClass('tm-img-prop', tm_selector)) {
											size_array["size"] = "未定义";
										}else {
											var m  = tm_selector.getElementsByTagName('dt')[0].innerHTML;
											select_array = tm_selector.getElementsByTagName('span');
											var sizeArray = [];
											for(var i = 0; i < select_array.length; i++) {
												sizeArray[i] = select_array[i].innerHTML;
											}
											size_array[m] = sizeArray;
										}
									}
								}
								return size_array;
							}, tmOrTaobao);
							goods_data["goods_size"] = size;
						
						
							//get the color
							var color = page.evaluate(function(flag_str) {
								var selector_dl, color_data = {}, color_array = [];
								if(flag_str == "taobao") {
									selector_dl = document.getElementsByClassName('J_Prop_Color')[0];
								}else {
									selector_dl = document.getElementsByClassName('tm-img-prop')[0];
								}
								if(selector_dl == undefined) {
									color_data['color'] = "未定义";
								}else {
									var colorKey = selector_dl.getElementsByTagName('dt')[0].innerHTML;
									var colorArray = selector_dl.getElementsByTagName('span');
									for(var i = 0; i < colorArray.length; i++) {
										color_array[i] = colorArray[i].innerHTML;
									}
									color_data[colorKey] = color_array;
								}
								return color_data;
							}, tmOrTaobao);
							goods_data["goods_color"] = color;
						
							//get the count evaluation
							t = window.setInterval(function(){
								var count_evaluation = page.evaluate(function() {
									var count;
									if(document.querySelectorAll("#J_tabBar .J_ReviewsCount").length == 0) {
										count = document.querySelectorAll("#J_TabBar .J_ReviewsCount");
									}else {
										count = document.querySelectorAll("#J_tabBar .J_ReviewsCount");
									}
									var eva_data = [];					
									if(typeof count[0] != "undefined") {
										eva_data[0] = count[0].innerText;
										return eva_data;
									}
								})
								if(count_evaluation != null) {
									clearInterval(t);
									goods_data["count_evaluation"] = count_evaluation;
								}
							}, 200);
							//图片的URL
							function checkReadyState() {
								var timer = setInterval(function () {
									var readyState = page.evaluate(function () {
										return document.readyState;
									});
									if ("complete" === readyState) {
										var link_array = page.evaluate(function() {
											//判断是否包含某个class名
											function hasClass(className,node){
												var classNames = node.className.split(/\s+/);
												for(var i = 0; i < classNames.length; i++){
													if(classNames[i] == className){
														return true;
													}
												}
												return false;
											}
											var wrap = document.getElementById("description");
											var img = document.querySelectorAll("#description img");
											var img_array = [], index = 0;
											if(typeof img != 'undefined' && img.length >= 1){
												var img_list =  document.querySelectorAll("#description img");
												for(var m = 0; m < img_list.length; m++) {
													var img_url;
													if(img_list[m].getAttribute('data-ks-lazyload') == null) {
														img_url = img_list[m].src;
													}else {
														img_url = img_list[m].getAttribute('data-ks-lazyload');
													}
													if(hasClass('desc_anchor', img_list[m]) == true){
													}else {
														img_array[index++] = img_url;
													}
												}
												img_array[img_array.length] = true;
						          			}
											return img_array;
										});
										//数组去重和删除start图片
										function removeDuplicatedItem(ar) {
											var ret = [];
											for(var i = 0; i <ar.length; i++) {
												if (ret.indexOf(ar[i]) === -1 && ! (/\?{1}/gi.test(ar[i]))) {
													ret.push(ar[i]);
												}
											}
											return ret;
										}
										if(link_array[link_array.length-1] == true){
											clearInterval(timer);
											link_array.length = link_array.length - 1;
											var img_linkArray = removeDuplicatedItem(link_array);
											goods_data.img_link = img_linkArray;
										}
									} else {
										checkReadyState();
									}
								}, 500);
							}
							checkReadyState();
						
							data_timer = setInterval(function() {
								var nowDate = new Date().getTime();
								if(nowDate > (parseInt(startTime) + 15 * 1000)) {
									clearInterval(data_timer);
									clearInterval(t);
									clearInterval(time);
									console.log("the page load more than 10s:"+ url);
									unable_str = "the page load is more than 10 seconds url is: " + url + "\r\n";
									fs.write('unable.txt', unable_str, 'a');
									return next(status, url);
								}
				    			if(goods_data["img_link"] != null & goods_data["goods_info"] != null && goods_data["count_evaluation"] !=null) {
					    			clearInterval(data_timer);
					    			var last = JSON.stringify(goods_data); 
					    			var path = "taobaotxt/" + tmOrTaobao + "_" + goodsId + ".txt";
					    			if(fs.exists(path)) {
					    				console.log("the txtFile is exists");
					    			}else {
					    				try{
									    	fs.write("taobaotxt/"+ tmOrTaobao + "_" + goodsId + ".txt", last, 'a');
									    }catch(e){
									    	console.log(e);
									    }
					    			}
					    			console.log("present url output data success");
					    			return next(status, url);
				    			}
				    		}, 200);
						}();
					}
				} else {
					return next(status, url);
				}
			});
		} else {
			return callbackFinal();
		}
	};
	return retrieve();
};
 
RenderUrlsToFile(str_Url, (function(status, url) {
	if (status !== "success") {
		unable_str = "the page is open faild url is: " + url + "\r\n";
		fs.write('unable.txt', unable_str, 'a');
		return console.log("fail open the page:" + url);
	}
}), function() {
    return phantom.exit();
});
